package com.michael.adapter.aspect;

import com.michael.adapter.protocol.HttpQueryProtocol;
import com.michael.common.error.code.BizCode;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
import org.springframework.util.ObjectUtils;

@Aspect
@Slf4j
@Component
public class TokenCheckAspect {
    @Around(value = "methodPointcut()")
    public Object around(ProceedingJoinPoint pjp) throws Throwable {
        try {
            String token = HttpQueryProtocol.getParam("token");
            if(ObjectUtils.isEmpty(token)){
                throw BizCode.TOKEN_ERROR.getException();
            }
            return pjp.proceed();
        } catch (Throwable e) {
            throw e;
        }
    }
    /**
     * 切入点入口
     */
    @Pointcut("annoPointcut()")
    public void methodPointcut() {
    }

    /**
     * 基于注解的切点
     */
    @Pointcut(value = "@annotation(com.michael.assist.anno.TokenCheck)")
    public void annoPointcut() {
    }
}
